package cn.itcast.demo.mq.hello;

import cn.itcast.demo.mq.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 发送者
 *
 * @author 虎哥
 */
public class SendDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 建立连接
        Connection connection = ConnectionUtils.getConnection();
        // 获取通道Channel
        Channel channel = connection.createChannel();
        // 创建队列
        channel.queueDeclare("hello.queue", false, false, false, null);
        // 发送消息
        String message = "hello, rabbitMQ!";
        channel.basicPublish("", "hello.queue", null, message.getBytes());
        System.out.println("消息已经发送： " + message);
        // 关闭连接和通道
        channel.close();
        connection.close();
    }
}
